// pages/datafill/datafill.js
var app = getApp();
var API_URL = app.globalData.API_URL;
Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id == undefined || options.id == '')
      {
        var id = 0;
      }else{
        var id = options.id;
      }
      var that = this;
      var uuid = wx.getStorageSync('uuid');
      wx.request({
        url: API_URL + 'my/info',
        method: "POST",
        data: {uuid: uuid },
        header: {
          'content-type': 'application/x-www-form-urlencoded', // 默认值
          'token': wx.getStorageSync('token'),
          'UUID': wx.getStorageSync('uuid')
        },
        success: function (res) {
          var uInfo = res.data.data;
          // console.log(uInfo)
          that.setData({
            uInfo: uInfo,
            phone: uInfo.phone,
            realname: uInfo.realname,
            province: uInfo.province,
            city: uInfo.city,
            area:uInfo.area,
            street: uInfo.street,
            region: [uInfo.province ? uInfo.province : '全部', uInfo.city ? uInfo.city : '全部', uInfo.area ? uInfo.area : '全部'],
            id: id
          })
        }
      });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
  formSubmit:function(e){
    var uuid = wx.getStorageSync('uuid');
    // console.log(e.detail.value);
      var companyName = e.detail.value.companyName;
      if (companyName == undefined || companyName == '') {
        wx.showToast({
          title: '请填写机构名称',
          image: '/images/erro.png',
          duration: 2000
        })
        return ;
      }
      var wechatno = e.detail.value.wechatno;
      // console.log(wechatno);
      if (wechatno == undefined || wechatno == '') {
        wx.showToast({
          title: '请填写微信号码',
          image: '/images/erro.png',
          duration: 2000
        })
        return ;
      }
      var contactPhone = e.detail.value.contactPhone;
      if (contactPhone == undefined || contactPhone == '') {
        wx.showToast({
          title: '请填写联系方式',
          image: '/images/erro.png',
          duration: 2000
        })
        return ;
      }
      var email = e.detail.value.email;
      if (email == undefined || email == '') {
        wx.showToast({
          title: '请填写邮箱',
          image: '/images/erro.png',
          duration: 2000
        })
        return ;
      }
     
      
      //提交数据
      wx.request({
        url: API_URL + 'my/setInfo2',
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'token': wx.getStorageSync('token'),
          'UUID': wx.getStorageSync('uuid')
        },
        data: {
          companyName: companyName,
          wechatno: wechatno,
          email: email,
          contactPhone:contactPhone,
          uuid: uuid
        },
        success: function (res) {
          var result = res.data;
          console.log(result);
          if (result.code == 1) {
            wx.showToast({
              title: '提交成功',
              icon: 'success',
              duration: 3000
            })
            //保存成功后跳转
            wx.switchTab({
              url: '/pages/myself/myself',
            })
          } else {
            wx.showToast({
              title: result.msg,
              image: '/images/erro.png',
              duration: 2000
            })
          }
        }
      })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})